package com.example.myfirst.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.myfirst.entity.Goods;
import com.example.myfirst.service.GoodsService;
import com.example.myfirst.vo.PageVo;
import com.example.myfirst.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @ApiOperation("获取所有商品信息")
    @GetMapping
    public PageVo getAllGoods(String goodsName, Integer CategoryId,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "8") Integer pageSize) {
        IPage<Goods> page = goodsService.queryAllGoods(goodsName, CategoryId, pageNum, pageSize);
        List<Goods> goodsList = page.getRecords();
        // 获取数据的总长度
        long total = page.getTotal();
        return new PageVo(goodsList, pageNum, total);
    }

    @ApiOperation("根据id获取商品信息")
    @GetMapping("/{id}")
    public ResultVo getGoodsById(@PathVariable Integer id) {
        Goods goods = goodsService.queryGoodsById(id);
        return new ResultVo(goods);
    }

    @ApiOperation("根据goodsName模糊查找商品信息")
    @GetMapping("/getByGoodsName/{goodsName}")
    public ResultVo getByGoodsName(@PathVariable String goodsName) {
        List<Goods> goods = goodsService.queryGoodsByGoodsName(goodsName);
        return new ResultVo(goods);
    }

    @ApiOperation("添加商品信息")
    @PostMapping
    public ResultVo addGoods(@RequestBody Goods goods) {
        Boolean flag = goodsService.addGoods(goods);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("添加商品成功");
            vo.setSuccess(true);
        } else {
            vo.setMessage("添加商品失败~");
            vo.setSuccess(false);
        }
        vo.setCode(200);
        return vo;
    }

    @ApiOperation("根据id删除商品信息")
    @DeleteMapping("/{id}")
    public ResultVo delGoods(@PathVariable Integer id) {
        Boolean flag = goodsService.delGoods(id);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("删除商品成功");
            vo.setSuccess(true);
        } else {
            vo.setMessage("删除商品失败~");
            vo.setSuccess(false);
        }
        vo.setCode(200);
        return vo;
    }

    @ApiOperation("根据id修改商品信息")
    @PutMapping("/{id}")
    public ResultVo updateGoodsById(@RequestBody Goods goods, @PathVariable Integer id) {
        Boolean flag = goodsService.updateGoodsById(goods, id);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("修改商品成功");
            vo.setSuccess(true);
        } else {
            vo.setMessage("修改商品失败~");
            vo.setSuccess(false);
        }
        vo.setCode(200);
        return vo;
    }

    @ApiOperation("根据系列id获取所有的商品")
    @GetMapping("/search")
    public ResultVo queryGoodsByCategoryId(Integer categoryId) {
        List<Goods> goodsList = goodsService.queryGoodsByCategoryId(categoryId);
        return new ResultVo(goodsList);
    }
}
